package com.tyj.movie.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;

/**
 * 日期: 2021/7/15 10:50
 * 作者: 谭友杰
 * 类说明:
 */
@Configuration
public class RabbitConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     *配置mq的存储格式  需要开启   publisher-returns: true
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 执行流程
     * 1. 先发送消息到延迟队列
     * 2. 当延迟队列时间到了就会通过设置的参数在此通过路由到队列
     * 3. 就可以监听该路由到的队列 获取消息
     */

    /**
     * 创建一个死信交换机
     */
    @Bean
    Exchange createDeadLetterExchange(){
        Exchange exchange=new DirectExchange("order-event-exchange",true,false);
        return exchange;
    }

    /**
     * 创建一个延迟队列
     */
    @Bean
    Queue createDeadLetterQueue(){
        HashMap<String, Object> arguments = new HashMap<>();
        //死信交换机
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        //死信路由键
        arguments.put("x-dead-letter-routing-key", "send-dead-letter-close-order");
        // 消息过期时间 5分钟 以毫秒为单位
        arguments.put("x-message-ttl", 300000);
        Queue queue = new Queue("dead-letter-queue", true, false, false, arguments);
        return queue;
    }

    /**
     * 关闭订单队列
     */
    @Bean
    Queue createCommonQueue(){
        Queue queue = new Queue("close-order-queue",true,false,false,null);
        return queue;
    }

    /**
     *
     */
    @Bean
    Binding creatBindingDeadLetter(){
        Binding binding = new Binding("dead-letter-queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "close-order",
                null);
        return binding;
    }

    @Bean
    Binding creatBindingCommonQueue(){
        Binding binding = new Binding("close-order-queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "send-dead-letter-close-order",
                null);
        return binding;
    }



    //创建订单
    @Bean
    Queue  createOrderQueue(){
        Queue queue=new Queue("create-order-queue");
        return queue;
    }

    @Bean
    Binding createOrderQueueBinding(){
        Binding binding=new Binding("create-order-queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "send-order",
                null);
        return binding;
    }

    @Bean
    Queue createRefundQueue(){
        Queue queue=new Queue("refund-order-queue");
        return queue;
    }

    @Bean
    Binding createRefundQueueBinding(){
        Binding binding=new Binding("refund-order-queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "refund-order",
                null);
        return binding;
    }

    @PostConstruct //RabbitMQConfig构造器创建完后,执行此方法
    public void initRabbitTemplate(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             *1只要消息抵达mq服务器回调
             * @param correlationData 消息的唯一关联数据(这个是消息的唯一id)
             * @param b 消息是否成功收到
             * @param s 失败的原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("confirm 服务端以抵达:correlationData="+correlationData+"   b=["+b+"]   s="+s);
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            /**
             * 只要消息没有投递给指定队列,就触发这个失败回调
             * @param message    投递失败的消息详细信息
             * @param replyCode  回复的状态码
             * @param replyText  回复的文本内容当
             * @param exchange   时这个消息发给哪个交换机
             * @param routingKey 当时发送的路由键
             */
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("returnedMessage message="+message+"   replyCode="+replyCode+"     replyText="+replyText+"     exchange="+exchange+"   routingKey="+routingKey );
            }
        });

    }

}
